package cn.kgc.crowdfundingadmin02.secrity;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2020-03-06 7:59
 */
@Configuration  //这一个配置类等同于原来的配置文件
@EnableWebSecurity  //启动security
@EnableGlobalMethodSecurity(prePostEnabled = true)//启用全局方法权限管理功能
public class MySecutityConfig extends WebSecurityConfigurerAdapter {



    @Resource
    UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //使用
        //放行首页、静态资源
        http.authorizeRequests()//对请求进行授权
        .antMatchers("/static/**","/index")//使用ant风格授权的url地址
        .permitAll()//允许上面使用ant风格设置的全部请求

//                .antMatchers("/admin/query/for/search")
//                .hasAnyAuthority("user:get")


        .anyRequest()//其他未设置的qingq
        .authenticated()//需要认证

        //2.1开启表单登录功能并前往登录表单页面
        .and()
        .formLogin()//设置未授权的请求跳转到登录页面
        .loginPage("/admin/to/login/page.html")
        .permitAll()//为登录页面设置所有人都可以访问


        .loginProcessingUrl("/admin/secutity/do/login")//指定登陆地址处理,不用原来的登陆了
        .usernameParameter("loginAcct")//指定页面参数
        .passwordParameter("userPswd")
        .defaultSuccessUrl("/admin/to/main/page.html")//登陆成功后跳转到

        .failureUrl("/admin/to/login/page.html?error=true")//登陆失败跳转url,并传参进行判断


                .and()
                .logout()//启动退出功能
                .logoutUrl("/admin/security/logout")//指定退出地址处理,不在使用原来的控制器
                .logoutSuccessUrl("/index")


                .and()
                .csrf()
                .disable() // 禁用CSRF功能，如果不禁用，只能发post请求退出，否则报错
        ;

        http.rememberMe().rememberMeParameter("remember");
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //认证

//
//        auth.inMemoryAuthentication()
//                .passwordEncoder(new BCryptPasswordEncoder())
//                .withUser("test")
//                .password(new BCryptPasswordEncoder().encode("123456"))
//                .roles("vip1");


        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());

    }
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

}
